import { Component, OnInit, ViewChild } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { STColumn, STComponent, STPage } from '@delon/abc/table';
import { SFSchema, SFComponent, SFRadioWidgetSchema } from '@delon/form';
import { LbWjfwComponent } from '@shared/components/lb_wjfw/lbwjfw.component';
import { LbStepsComponent } from '@shared/components/lb-steps/lb-steps.component';
import { ReuseTabService } from '@delon/abc';
import { ActivatedRoute, Router } from '@angular/router';
import { SupDic, HttpService } from 'lbf';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { ZyjkModule } from '../zyjk.module';
import { type } from 'os';
import { LbRepComponent } from '@shared/components/lb-rep/lb-rep.component';

@Component({
  selector: 'app-zyjk-zcdj',
  templateUrl: './zcdj.component.html',
})
export class ZyjkZcdjComponent implements OnInit {
  tabs = ['办件信息', '材料影像'];
  dataset = [];
  PROJID = '1';
  fordate: any = {};
  DAD069 = '';
  isVisible = false;
  isOkLoading = false;
  old_BMC031 = '';
  dataModal = [];
  fordate1232 = {};
  params = { sname: 'BJXX_QUERY', queryparas: {} };
  isOkLoading1 = false;
  isVisible1 = false;

  slSchema: SFSchema = {
    properties: {
      DAD095: {
        type: 'string',
        title: '',
        enum: [
          { label: '受理', value: '1' },
          { label: '不受理', value: '0' },
        ],
        // tslint:disable-next-line: no-object-literal-type-assertion
        ui: {
          widget: 'radio',
          // asyncData: () => of().pipe(delay(100)),
          change: console.log,
          grid: {
            span: 24,
          },
        } as SFRadioWidgetSchema,
        default: '1',
      },
      DAD098: {
        type: 'string',
        title: '不予受理原因',
        ui: {
          visibleIf: { DAD095: ['0'] },
          spanLabelFixed: 150,
          grid: { span: 24 },
        },
      },
    },
    ui: { grid: { span: 24 } },
    required: ['DAD098'],
  };

  constructor(
    private lbservice: HttpService,
    public msgSrv: NzMessageService,
    private supDic: SupDic,
    private reuseTabService: ReuseTabService,
    private activateroute: ActivatedRoute,
    private router: Router,
    private modalService: NzModalService,
  ) {
    this.reuseTabService.title = '基本医疗保险转出登记';
  }

  bjxxSchema: SFSchema = {
    properties: {
      AAC002: {
        type: 'string',
        title: '身份证号',
        readOnly: true,
      },

      AAC003: {
        type: 'string',
        title: '姓名',
        readOnly: true,
      },

      AAC067: {
        type: 'string',
        title: '手机号码',
        readOnly: true,
      },

      AAB301: {
        type: 'string',
        title: '转入地行政区划代码',

        ui: { hidden: true },
      },
      AAC027: {
        type: 'string',
        title: '转出地行政区划代码',
        enum: this.supDic.getSFDic('AAB301'),
        readOnly: true,
        ui: {
          widget: 'select',
        },
      },

      BAE011: {
        title: '省内省外转移标志',
        type: 'string',
        enum: this.supDic.getSFDic('BAE011'),
        readOnly: true,
        ui: {
          widget: 'select',
        },
      },
    },
    ui: {
      spanLabelFixed: 150,
      grid: { span: 8 },
    },
  };

  // dac133
  bjkkkSchema: SFSchema = {
    properties: {
      DAC133: {
        title: 'DAC133',
        type: 'string',
        ui: { hidden: true },
      },
    },
  };

  // 本地获取数据
  sffschema: SFSchema = {
    properties: {
      AAE173: {
        title: '账户类别',
        type: 'string',
        enum: [
          { label: '转出', value: '2' },
          { label: '转入', value: '1' },
        ],
        default: '2',
        readOnly: true,

        ui: { widget: 'select' },
      },
      AAE473: {
        title: '转移账户类别',
        type: 'string',
        enum: this.supDic.getSFDic('AAE473'),
        default: '1',
        readOnly: true,
        ui: {
          widget: 'select',
        },
      },

      AAE140: {
        title: '险种类型',
        type: 'string',
        enum: [{ label: '职工医疗保险', value: '310' }],
        default: '310',
        readOnly: true,
      },

      AAC105: {
        type: 'string',
        title: '转移范围',
        enum: [{ label: '跨省', value: '1' }],
        default: '1',
        readOnly: true,
      },

      SBJG: {
        title: '对方社保机构名称',
        type: 'string',
        ui: { spanLabelFixed: 150, grid: { span: 12 } },
      },
      AAE008: {
        title: '开户银行行号',
        type: 'string',
        ui: { hidden: true },
      },
      AAE010: {
        title: '银行账号',
        type: 'string',
        ui: { hidden: true },
      },
    },
    ui: {
      spanLabelFixed: 150,
      grid: { span: 8 },
    },
    required: ['SBJG'],
  };

  tabMode = 'bjxx';
  @ViewChild('st', { static: false }) st: STComponent;
  @ViewChild('stdd', { static: false }) stdd: STComponent;
  @ViewChild('sf', { static: false }) sf: SFComponent;
  @ViewChild('slxx', { static: false }) slxx: SFComponent;
  @ViewChild('wjfw', { static: false }) wjfw: LbWjfwComponent;
  @ViewChild('step', { static: true }) step: LbStepsComponent;
  @ViewChild('sffd', { static: false }) sffd: SFComponent;

  clsmcolumns: STColumn[] = [
    { title: '序号', index: 'RN', width: '10%' },
    { title: '材料名称', index: 'STUFFNAME', width: '50%' },
    { title: '材料数量', index: 'STUFFNUM', width: '25%' },
    { title: '收件类型', index: 'STUFFTYPE', width: '15%' },
  ];

  clsmModal: STColumn[] = [
    { title: '编号', index: 'TID', width: '10%' },
    { title: '对方社保机构名称 ', index: 'SBJG', width: '50%' },
    { title: '社保机构编码 ', index: 'SBJGBM', width: '20%' },
    {
      buttons: [
        {
          type: 'drawer',
          text: '添加',
          click: record => this.handleOk(record),
        },
      ],
    },
  ];

  page: STPage = {
    show: false,
  };

  ngOnInit() {
    if (this.activateroute.snapshot.queryParams.PROJID) {
      this.PROJID = this.activateroute.snapshot.queryParams.PROJID;
      this.DAD069 = this.activateroute.snapshot.queryParams.DAD069;
      this.YB_QUERY();
      this.YBCL_QUERY();
    }
  }

  // 打开情况下再次传入值
  _onReuseInit(): void {
    if (this.activateroute.snapshot.queryParams.PROJID) {
      this.PROJID = this.activateroute.snapshot.queryParams.PROJID;
      this.DAD069 = this.activateroute.snapshot.queryParams.DAD069;
      this.YB_QUERY();
      this.YBCL_QUERY();
    }
  }

  // 事项进度
  selectTab(str: string) {
    this.tabMode = str;
    if (str === 'sxjd') {
    }
  }

  // 界面赋值
  YB_QUERY() {
    this.lbservice
      .lbservice('YB_QUERY', { para: { PROJID: this.PROJID } })
      .then(resdata => {
        if (resdata.code < 1) {
          this.msgSrv.error(resdata.errmsg);
        } else {
          this.sf.formData = JSON.parse(resdata.message);
          this.sf.reset();
        }
      });
  }

  // 材料列表
  YBCL_QUERY() {
    this.lbservice
      .lbservice('YBCL_QUERY', {
        para: { PROJID: this.PROJID },
      })
      .then(resdata => {
        if (resdata.code === 0) {
          this.msgSrv.error(resdata.errmsg);
        } else {
          this.dataset = resdata.message.list;
        }
      });
  }

  // 受理保存
  // lc_insert(value1: any) {
  //   // tslint:disable-next-line: all
  //   if (value1.DAD095 == '0') {
  //     if (!this.slxx.valid) {
  //       return this.msgSrv.error('没有填写必填项');
  //     }
  //     this.lbservice
  //       .lbservice('pk_zcdj', {
  //         para: {
  //           PROJID: this.PROJID,
  //           sf: this.sf.value,
  //           slxx: this.slxx.value,
  //           sffd: this.sffd.value,
  //           DAD069: this.DAD069,
  //         },
  //       })
  //       .then(resdata => {
  //         if (resdata.code < 1) {
  //           this.msgSrv.error(resdata.errmsg);
  //         } else {
  //           this.sffd.formData = null;
  //           this.msgSrv.info('登记成功');
  //           this.sffd.reset();
  //         }
  //       });
  //   } else {
  //     if (!this.sffd.valid) {
  //       return this.msgSrv.error('没有填写必填项');
  //     }

  //     this.lbservice
  //       .lbservice('pk_zcdj', {
  //         para: {
  //           PROJID: this.PROJID,
  //           sf: this.sf.value,
  //           slxx: this.slxx.value,
  //           sffd: this.sffd.value,
  //           DAD069: this.DAD069,
  //         },
  //       })
  //       .then(resdata => {
  //         if (resdata.code < 1) {
  //           this.msgSrv.error(resdata.errmsg);
  //         } else {
  //           this.msgSrv.info('登记成功');
  //           this.sffd.formData = null;
  //           this.sffd.reset();
  //           this.isVisible1 = true;
  //         }
  //       });
  //   }
  // }

  //受理保存
  lc_insert(value1: any) {
    this.lbservice
      .lbservice('pk_zcdj', {
        para: {
          PROJID: this.PROJID,
          sf: this.sf.value,
          slxx: this.slxx.value,
          sffd: this.sffd.value,
          DAD069: this.DAD069,
        },
      })
      .then(resdata => {
        if (this.sffd.valid == true && this.sf.valid == true) {
          if (resdata.code < 1) {
            this.msgSrv.error(resdata.errmsg);
          } else {
            this.msgSrv.info('保存成功');
            //保存后调打印
            this.isVisible1 = true;
          }
        } else {
          //事项受理时必填  不予受理时不需要必填
          if (this.slxx.value.DAD095 == '1') {
            this.msgSrv.info('必填项请填写');
          } else {
            if (!this.slxx.valid) {
              this.msgSrv.info('不予受理原因请填写');
            } else {
              if (resdata.code < 1) {
                this.msgSrv.error(resdata.errmsg);
              } else {
                this.msgSrv.info('该事项已不予受理');
              }
            }
          }
        }
        //保存后数据清空
        this.sf.formData = null;
        this.slxx.formData = null;
        this.slxx.reset();
        this.sf.reset();
        this.sffd.formData = null;
        this.sffd.reset();
      });
  }

  //
  onEnter(para: any) {
    // tslint:disable-next-line: no-conditional-assignment
    if ((para.SBJG = this.old_BMC031)) {
      return this.msgSrv.error('医保机构不能为空');
    }

    this.lbservice
      .lbservice('L_YB_aa33', {
        para: { sf: this.sffd.value },
      })
      .then(resdata => {
        if (resdata.code < 1) {
          this.msgSrv.error(resdata.errmsg);
        } else {
          this.dataModal = resdata.message.list;
          this.stdd.reset();
        }
      });

    this.isVisible = true;
  }

  handleCancel() {
    this.isVisible = false;
  }
  handleOk(value: any) {
    this.fordate.SBJG = value.SBJG;
    this.sffd.reset();

    this.isVisible = false;
  }

  //是否打印页面
  handleOk1(): void {
    this.isOkLoading1 = true;
    this.print();
    setTimeout(() => {
      this.isVisible1 = false;
      this.isOkLoading1 = false;
    }, 3000);
  }
  handleCancel1(): void {
    this.isVisible1 = false;
  }
  print() {
    console.log('单号：' + this.DAD069);
    const modal = this.modalService.create({
      nzTitle: '打印',
      nzContent: LbRepComponent,
      nzComponentParams: {
        repid: '51',
        inpara: { DAD069: this.DAD069 },
      },
      nzWidth: 1000,
    });
  }

  // tslint:disable-next-line: member-ordering
  pl = {
    pname: '省端传入数据',
    active: true,
    pname1: '本地数据',
    active1: false,
    icon1: 'caret-right',
  };
}
